from flask import Flask
from flask import render_template
from flask import request
from flask import session, redirect, url_for, request

app = Flask(__name__)
app.secret_key = '123456'

@app.route('/')
def index():
    if 'username' in session:
        username = session['username']
        return '登录用户名是:' + username + '<br/>' + '<a href="/logout">登出</a>'
    return '您暂未登录账户！请<a href="/login">登录</a>'

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    
    elif request.method == 'GET':
        return '''
            <form action="" method="post">
            <p><input type=text name=username>
            <p><input type=submit value=登录>
            </form>
        '''
    
@app.route('/logout')
def logout():
    session.pop('username', None)
    return redirect(url_for('index'))
   

if __name__ == '__main__':
    app.run(debug=True, threaded=True)